package org.example.test1;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.Map;

public class QuarterGroup extends Group implements RangeGenerable {

    public QuarterGroup() {
    }

    /**
     * 季度组
     * @param date
     */
    public QuarterGroup(LocalDate date) {
        super(date);
    }

    @Override
    public String getDisplayName() {
        int quarter = (date.getMonthValue() - 1) / 3 + 1;
        return String.format("%d-Q%d", date.getYear(), quarter);
    }

    @Override
    public int compareTo(LocalDate o) {
        int quarterThis = (date.getMonthValue() - 1) / 3 + 1;
        int quarterOther = (o.getMonthValue() - 1) / 3 + 1;
        return date.getYear() == o.getYear() && quarterThis == quarterOther ? 0 : -1;
    }

    @Override
    public Map<Group, Counter> generate(LocalDate start, LocalDate end) {
        return new HashMap<Group, Counter>() {{
            LocalDate current = start.with(TemporalAdjusters.firstDayOfMonth());
            while (!current.isAfter(end)) {
                put(new QuarterGroup(current), new Counter(0, 0));
                current = current.plusMonths(3).with(TemporalAdjusters.firstDayOfMonth());
            }
        }};
    }
}